<?php
/**
 * Created by PhpStorm.
 * User: Smlie
 * Date: 2016/5/5
 * Time: 16:28
 */
namespace Admin\Controller;

use Think\Controller;
use Think\Log;

class DepositRecordController extends BaseController{

    public function index(){
        $count_checked=M('user_pre_deposit_record')->where(array('is_checked'=>0))->count();
        $this->assign('count_checked', $count_checked);
        $this->display();
    }


    public function index_list(){

        $_listrows = I('request._listrows', '', 'trim');
        $user_name = I("request.user_name");
        $is_checked = I("request.is_checked");
        $where=array();
        if (!empty($user_name)) {
            $where['user_name'] = array('like', "%$user_name%");
        }
        if (is_numeric($is_checked)) {
            $where['is_checked'] = $is_checked;
        }
        $_where_order_field['id']='load_tb_div';
        $_where_order_field['listrows'] = $_listrows;
        $_where_order_field['order']='add_time desc';
        $_where_order_field['where'] = $where;
        $_where_order_field['alias']=' up';
        $_where_order_field["join"]=" ki_users AS users ON users.user_id = up.user_id ";
        $_where_order_field["field"]=" up.*,user_name";
        $_datalist = $this->_getListsFromModel('user_pre_deposit_record', $_where_order_field);
        $user_DB=M('users');
        foreach ($_datalist["lists"] as $key => $value) {
            $value["users"] =$user_DB ->where(array("user_id" => $value["user_id"]))->find();
            $_datalist["lists"][$key] = $value;
        }

        $this->assign('_datalist', $_datalist);
        $this->display();
    }


    public function checked(){

        $id=I('id');
        if(IS_POST){
            $is_checked=I('is_checked');//审核状态

            $check_remark=I('check_remark');//审核备注
            if($is_checked==2){
                if(empty($check_remark)){
                    $this->error('审核拒绝须填写审核备注！');
                }
            }

            $data['is_checked']=$is_checked;
            $data['check_user']=UID;//审核人
            $data['check_time']=time();//审核时间
            $data['check_remark']=$check_remark;

            M("user_pre_deposit_record")->where(array("id"=>$id))->save($data);


            if($is_checked==1){

                $user_id = I('request.user_id','','trim');
                $users = M('users')->where(array('user_id'=>$user_id))->field('mobile_phone,user_money,user_name')->find();

                $money = I('request.money',0,'doubleval');

                $da['source_data_id']=0;
                $da['source_type']=1;
                $da['transaction_type']=1;
                $da['in_or_out']='IN';
                $da['user_id']=$user_id;
                $da['user_name']=$users['user_name'];
                $da['mobile']=$users['mobile_phone'];
                $gathering_man=I('gathering_man');
                if(empty($gathering_man)){
                    $this->error('请输入收款人');
                }
                $da['gathering_man']=$gathering_man;
                $bank_transaction_id=I('bank_transaction_id');
                if(empty($bank_transaction_id)){
                    $this->error('请输入银行流水号/收据号');
                }
                $da['bank_transaction_id']=$bank_transaction_id;
                $da['reference']=I('reference');
                $da['add_time']=time();
                $da['gathering_way'] =2;   //充值类型，1：现金，2：转账
                $gathering_time=date("Y-m-d H:i:s",time());//最后操作时间
                if(empty($gathering_time)){
                    $this->error('请选择到款时间');
                }
                $da['gathering_time'] = $gathering_time;
                $da['money'] = $money;//充值金额
                $da['gathering_money']=I('gathering_money');//收款充值

                $pl = M('purchase_price_list')->add($da);

                if( $pl ) {

                    $present_money_set =
                        M('present_money_set')
                            ->where(array(
                                'status' => 1,
                                'money_start' => array('elt', $money),
                                'money_end' => array('egt', $money),
                            ))->order('money_start desc')->find();
                    if (!empty($present_money_set)) {
                        $reward_percent = $present_money_set['send_money'] / $present_money_set['every_money'];
                        $reward_money = $money * $reward_percent;

                        $_POST['source_type'] = 3;
                        $_POST['transaction_type'] = 3;
                        $_POST['reference'] = '赠款充值';
                        $_POST['remark'] = '赠款充值';
                        $_POST['money'] = $reward_money;
                        $_POST['gathering_time'] = date("Y-m-d H:i:s", time());//最后操作时间
                        $_POST['user_balance_details'] = $users['user_money'] + $reward_money + $money;//用户当前余额
                        M('purchase_price_list')->add($_POST);

                        $money = $money + $reward_money;
                    }
                    //累加user_money
                    M('users')->where(array('user_id' => $user_id))->setInc('user_money', $money);
                }
            }
            $exe_scriptstr = "<script>
                setTimeout(function(){
					var win=    art.dialog.opener;
                               win.load();
                                art.dialog.close();
                },1000);
           </script>";


            //添加成功后，输出文本，把js脚本一起输出，前台只要html到某个div里就能够执行js
            $this->success('操作成功！' . $exe_scriptstr);

        }else{
            $entity=M("user_pre_deposit_record")->where(array("id"=>$id))->find();

            //获取用户信息
            $entity['users']=M('users')->field('user_name,mobile_phone')->where(array('user_id'=>$entity['user_id']))->find();

            $this->assign('entity',$entity);
            $this->display();
        }

    }

    public function details(){
        $id=I('id');
        $entity=M("user_pre_deposit_record")->where(array("id"=>$id))->find();

        //获取用户信息
        $entity['users']=M('users')->field('user_name,mobile_phone')->where(array('user_id'=>intval($entity['user_id'])))->find();

        //获取审核用户信息
        $entity['admin_user_name']=M('admin_user')->where(array('user_id'=>intval($entity['check_user'])))->getField('user_name');

        $this->assign('entity',$entity);
        $this->display();


    }

}